package com.ruoyi.system.constant;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Basetrans;
import com.ruoyi.system.domain.TbListExpass;
import com.ruoyi.system.domain.TbListOthertrans;
import com.ruoyi.system.service.IBasetransService;
import com.ruoyi.system.service.ITbListExpassService;
import com.ruoyi.system.service.ITbListOthertransService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @author lzj
 */
@Service
@Slf4j
public class AsyncService {

    @Autowired
    private IBasetransService basetransService;

    @Autowired
    private ITbListExpassService tbListExpassService;

    @Autowired
    private ITbListOthertransService othertransService;

    @Async
    public CompletableFuture<Void> processBasetrans(Basetrans basetrans, String exStartTime, String exEndTime) {
        TbListExpass tbListExpass = new TbListExpass();
//        tbListExpass.setExVehicleId(basetrans.getEtcVehiclePlate());
        tbListExpass.setCardId(basetrans.getEtcCardId());
        tbListExpass.setEnStationId(basetrans.getEntryStationId());
        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", exStartTime);
        params.put("endTime", exEndTime);
        params.put("exVehicleId", StringUtils.split(basetrans.getEtcVehiclePlate(), "_")[0]);
        tbListExpass.setParams(params);

        List<TbListExpass> tbListExpassList = tbListExpassService.selectTbListExpassList(tbListExpass);

        if (tbListExpassList.size() > 1) {
            for (TbListExpass exPass : tbListExpassList) {
                updateBasetrans(basetrans, exPass);
            }
        } else {
            if (!tbListExpassList.isEmpty()) {
                updateBasetrans(basetrans, tbListExpassList.get(0));
            } else {
                // 查otherTrans表
                TbListOthertrans othertrans = new TbListOthertrans();
//                othertrans.setExVehicleId(basetrans.getEtcVehiclePlate());
                othertrans.setEtcCardId(basetrans.getEtcCardId());
                othertrans.setEnStationId(basetrans.getEntryStationId());
                Map<String, Object> param = new HashMap<>();
                param.put("beginTime", exStartTime);
                param.put("endTime", exEndTime);
                param.put("exVehicleId", StringUtils.split(basetrans.getEtcVehiclePlate(), "_")[0]);
                othertrans.setParams(param);
                List<TbListOthertrans> othertransList = othertransService.selectTbListOthertransList(othertrans);
                if (othertransList.size() > 1) {
                    for (TbListOthertrans exPass : othertransList) {
                        updateBasetransByOtherTrans(basetrans, exPass);
                    }
                } else {
                    if (!othertransList.isEmpty()) {
                        updateBasetransByOtherTrans(basetrans, othertransList.get(0));
                    } else {
                        log.error("卡ID：[{}], 入口站：[{}-{}], 车牌号：[{}]", basetrans.getEtcCardId(), basetrans.getEntryStationName(), basetrans.getEntryStationId(), basetrans.getEtcVehiclePlate());
                    }
                }
            }
        }

        return CompletableFuture.completedFuture(null);
    }

    private void updateBasetrans(Basetrans basetrans, TbListExpass exPass) {
        if (Objects.equals(exPass.getFee(), basetrans.getTotalToll())) {
            Basetrans ent = new Basetrans();
            ent.setTradeId(basetrans.getTradeId());
            ent.setEtcCardId(basetrans.getEtcCardId());
            ent.setSpare3(1L);
            basetransService.updateBasetrans(ent);
        } else {
            Basetrans ent = new Basetrans();
            ent.setTradeId(basetrans.getTradeId());
            ent.setEtcCardId(basetrans.getEtcCardId());
            ent.setSpare3(2L);
            basetransService.updateBasetrans(ent);
            log.warn("比对失败信息：卡ID：[{}], 入口站：[{}-{}], 车牌号：[{}], 预交易金额：[{}], 线上金额：[{}]",
                    basetrans.getEtcCardId(), basetrans.getEntryStationName(), basetrans.getEntryStationId(),
                    basetrans.getEtcVehiclePlate(), basetrans.getTotalToll(), exPass.getFee());
        }
    }

    private void updateBasetransByOtherTrans(Basetrans basetrans, TbListOthertrans othertrans) {
        if (Objects.equals(othertrans.getFee(), basetrans.getTotalToll())) {
            Basetrans ent = new Basetrans();
            ent.setTradeId(basetrans.getTradeId());
            ent.setEtcCardId(basetrans.getEtcCardId());
            ent.setSpare3(1L);
            basetransService.updateBasetrans(ent);
        } else {
            Basetrans ent = new Basetrans();
            ent.setTradeId(basetrans.getTradeId());
            ent.setEtcCardId(basetrans.getEtcCardId());
            ent.setSpare3(2L);
            basetransService.updateBasetrans(ent);
            log.warn("比对失败信息：卡ID：[{}], 入口站：[{}-{}], 车牌号：[{}], 预交易金额：[{}], 线上金额：[{}]",
                    basetrans.getEtcCardId(), basetrans.getEntryStationName(), basetrans.getEntryStationId(),
                    basetrans.getEtcVehiclePlate(), basetrans.getTotalToll(), othertrans.getFee());
        }
    }
}
